<?php

namespace addons\Merchants\backend\controllers;

use Yii;
use common\enums\StatusEnum;
use common\helpers\ArrayHelper;
use common\helpers\EchantsHelper;
use common\models\merchant\CreditsLog;
use common\helpers\ResultHelper;
use common\helpers\DateHelper;

/**
 * Class StatController
 * @package addons\Merchants\backend\controllers
 * @author YiiFrame <21931118@qq.com>
 */
class StatController extends BaseController
{
    /**
     * @return string
     */
    public function actionIndex()
    {
        $merchant_id = Yii::$app->request->get('merchant_id', '');
        $merchant = Yii::$app->services->merchant->findBaseAll();
        $merchant = ArrayHelper::merge(['' => '全部'], ArrayHelper::map($merchant, 'id', 'title'));

        $today = DateHelper::today();

        return $this->render($this->action->id, [
            'merchantCommissionWithdraw' => Yii::$app->services->merchantCommissionWithdraw->getApplyCount($merchant_id),
            'merchantCount' => Yii::$app->services->merchant->getCount($merchant_id),
            'merchantApplyCount' => Yii::$app->services->merchant->getApplyCount($merchant_id),
            'merchantAccount' => Yii::$app->services->merchantAccount->getSum($merchant_id),
            'merchantTotalMoney' => Yii::$app->services->merchantCreditsLog->getBetweenMoney($today['start'], $today['end'],$merchant_id),
            'merchant_id' => $merchant_id,
            'merchant' => $merchant,
        ]);
    }

    /**
     * 收入统计
     *
     * @param $type
     * @return array|mixed
     */
    public function actionEarnings($type, $merchant_id = '')
    {
        $fields = [
            'price' => '收入统计',
        ];

        // 获取时间和格式化
        list($time, $format) = EchantsHelper::getFormatTime($type);
        // 获取数据
        $data = EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) use ($merchant_id) {
            $data = CreditsLog::find()
                ->select(['sum(num) as price', "from_unixtime(created_at, '$formatting') as time"])
                ->where(['credit_type' => CreditsLog::CREDIT_TYPE_USER_MONEY])
                ->andWhere(['>', 'num', 0])
                ->andWhere(['>', 'status', StatusEnum::DISABLED])
                ->andWhere(['between', 'created_at', $start_time, $end_time])
                ->andFilterWhere(['merchant_id' => $merchant_id])
                ->groupBy(['time'])
                ->asArray()
                ->all();

            foreach ($data as &$datum) {
                $datum['price'] = abs($datum['price']);
            }

            return $data;
        }, $fields, $time, $format);

        return ResultHelper::json(200, '获取成功', $data);
    }

    /**
     * 收入统计
     *
     * @param $type
     * @return array|mixed
     */
    public function actionWithdraw($type, $merchant_id = '')
    {
        $fields = [
            'price' => '支出统计',
        ];

        // 获取时间和格式化
        list($time, $format) = EchantsHelper::getFormatTime($type);
        // 获取数据
        $data = EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) use ($merchant_id) {
            $data = CreditsLog::find()
                ->select(['sum(num) as price', "from_unixtime(created_at, '$formatting') as time"])
                ->where(['credit_type' => CreditsLog::CREDIT_TYPE_USER_MONEY])
                ->andWhere(['<', 'num', 0])
                ->andWhere(['>', 'status', StatusEnum::DISABLED])
                ->andWhere(['between', 'created_at', $start_time, $end_time])
                ->andFilterWhere(['merchant_id' => $merchant_id])
                ->groupBy(['time'])
                ->asArray()
                ->all();

            foreach ($data as &$datum) {
                $datum['price'] = abs($datum['price']);
            }

            return $data;
        }, $fields, $time, $format);

        return ResultHelper::json(200, '获取成功', $data);
    }
}